package top.hkyzf.study.j2se.other;

import org.junit.Test;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 朱峰
 * @date 2020-12-24 15:36
 */
public class T {
    @Test
    public void testString() {
        String str1="hello";
        String str2="he" + new String("llo");
        System.err.println(str1==str2);
        StringBuilder sb = new StringBuilder();
        String ss = sb.append("he").append("llo").toString();
    }

    @Test
    public void testIntegerAndInt () {

        int int1 = 12;
        int int2 = 12;
        Integer Integer1 = new Integer(12);
        Integer Integer2 = new Integer(12);
        Integer Integer3 = new Integer(127);

        Integer a1 = 127;
        Integer b1 = 127;

        Integer a = 128;
        Integer b = 128;
        int aa = 128;

        String s1 = "str";
        String s2 = "str";
        String str1 = new String("str");
        String str2 = new String("str");

        System.out.println("int1==int2:" + (int1 == int2));
        System.out.println("int1==Integer1:" + (int1 == Integer1));
        System.out.println("Integer1==Integer2:" + (Integer1 == Integer2));
        System.out.println("Integer3==b1:" + (Integer3 == b1));
        System.out.println("a1==b1:" + (a1 == b1));
        System.out.println("a==b:" + (a == b));
        System.out.println("a==aa:" + (a == aa));


        System.out.println("s1==s2:" + (s1 == s2));
        System.out.println("s1==str1:" + (s1 == str1));
        System.out.println("str1==str2:" + (str1 == str2));

    }

    @Test
    public void testArrayCopy () {
        int[] a = new int[10];
        a[0] = 0;
        a[1] = 1;
        a[2] = 2;
        a[3] = 3;
        // 把 2，3，4 三个位置的数据拷贝到 3，4，5 的位置
        System.arraycopy(a, 2, a, 3, 3);
        // a[2]=99;
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
    }

    @Test
    public void testLock () {
        Phone phone = new Phone();
        Thread t = new Thread(phone, "T1");
        t.start();

        Thread t1 = new Thread(phone, "T2");
        t1.start();
    }
}

class Phone implements Runnable{

    Lock lock = new ReentrantLock();
    @Override
    public void run() {
        get();
    }

    /**
     * 多次加锁，多次解锁
     */
    private void get() {
        lock.lock();
        lock.lock();
        try {
            System.out.println("get invoked");
            set();
        } finally {
            lock.unlock();
            lock.unlock();
        }
    }

    private void set() {
        lock.lock();
        try {
            System.out.println("set invoked");
        } finally {
            lock.unlock();
        }
    }
}
